<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <!-- 左 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRoleList"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="RoleList">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <!-- elui自带的插槽 row点击的当前行-->
                <template #default="{row}">
                  <el-button size="small" type="success" @click="perm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRoleList(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRoleList(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination layout="prev,pager,next" :total="total" :page-size="Query.pagesize" @current-change="currentChange" />
            </el-row>
          </el-tab-pane>
          <!-- 右 -->
          <el-tab-pane label="公司信息">
            <!-- 警告 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="CompanyInfo.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="CompanyInfo.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="CompanyInfo.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="CompanyInfo.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>
        <el-dialog :title="roleForm.id?'新增':'编辑'" :visible="showDialog" @close="btnCancel">
          <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="roleForm.name" />
            </el-form-item>
            <el-form-item label="角色描述" prop="description">
              <el-input v-model="roleForm.description" />
            </el-form-item>
          </el-form>
          <!-- 底部 -->
          <el-row slot="footer" type="flex" justify="center">
            <el-col :span="6">
              <el-button size="small" @click="btnCancel">取消</el-button>
              <el-button size="small" type="primary" @click="btnOK">确定</el-button>
            </el-col>
          </el-row>
        </el-dialog>
      </el-card>
      <el-dialog title="分配权限" :visible="isShowperm" @close="isShowperm=false">
        <el-tree ref="permTree" :data="premList" :props="{label:'name'}" :show-checkbox="true" :check-strictly="true" node-key="id" />
        <template #footer>
          <el-row type="flex" justify="end">
            <el-button size="small" type="primary" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-row>
        </template>
      </el-dialog>

    </div>
  </div>
</template>

<script>
import { add, editRoleList, delRoleList, getCompanyInfo, getRoleList, addRoleList, assignPermApi } from '@/api/setting'
import { getpermissonList } from '@/api/permisson.js'
import { listTotreeData } from '@/utils/index'

export default {
  data() {
    return {
      CompanyInfo: {},
      Query: {
        page: 1, // 页码
        pagesize: 2 // 每页条数
      },
      RoleList: [], // 所有角色信息:
      total: 0,
      showDialog: false,
      title: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { max: 10, message: '长度在 1到 10个字符', trigger: 'blur' }
        ],
        description: [{ required: true, message: '不能为空', trigger: 'blur' },
          { max: 10, message: '长度在 1到 10个字符', trigger: 'blur' }]
      },
      isShowperm: false,
      premList: [], // 权限列表
      roleId: ''
    }
  },
  created() {
    // 调用封装函数
    this.getCompanyInfo()
    this.getRoleList()
    this.getpermList()
  },
  methods: {
    // 确定分配权限
    async  btnPermOK() {
      // 发送请求
      await assignPermApi({
        id: this.roleId,
        permIds: this.$refs.permTree.getCheckedKeys()
      })
      // 提示用户
      this.$message.success('分配成功')
      //  关闭弹窗
      this.isShowperm = false
    },
    // 取消分配权限
    btnPermCancel() {
      // 清除数据
      this.$refs.permTree.setCheckedKeys([])
      // 关闭弹窗
      this.isShowperm = false
    },
    // 权限数据回显
    async  getpermList() {
      this.premList = listTotreeData(await getpermissonList(), '0')
    },
    // 分配权限
    async perm(id) {
      // 保存id、
      this.roleId = id
      // 解构
      const { permIds } = await editRoleList(id)
      // 显示弹窗
      this.isShowperm = true
      // 渲染权限
      this.$nextTick(() => {
        this.$refs.permTree.setCheckedKeys(permIds)
      })
    },
    // 封装请求函数
    async getCompanyInfo() {
      // 从vuex中传入id，保存数据
      this.CompanyInfo = await getCompanyInfo(this.$store.state.user.userInfo.companyId)
    },
    async getRoleList() {
      // 解 构
      const { rows, total } = await getRoleList(this.Query)
      // console.log(85, rows)
      this.RoleList = rows
      // 保存总条数
      this.total = total
    },
    // 点击分页
    currentChange(newpage) {
      // 修改页码
      this.Query.page = newpage
      // 发起请求
      this.getRoleList()
    },
    // 删除角色
    async delRoleList(id) {
      // 提示用户是否删除
      await this.$confirm('是否删除')
      // console.log(109, id)
      // 发起请求删除角色
      await delRoleList(id)
      // 重新渲染页面
      this.getRoleList()
      // 提示删除成功
      this.$message.success('删除成功')
    },
    // 新增角色
    addRoleList() {
      this.showDialog = true
    },
    // 编辑角色
    async  editRoleList(id) {
      // 保存数据有id，就可以用于判断title的标题
      this.roleForm = await editRoleList(id)
      // console.log(159, this.roleForm)
      // 显示弹窗
      this.showDialog = true
    },
    // 取消
    btnCancel() {
      // 清理数据
      this.roleForm = {
        name: '',
        description: ''
      }
      // 清理报错
      this.$refs.roleForm.resetFields()
      // 关闭弹窗
      this.showDialog = false
    },
    // 确定
    async btnOK() {
      // 校验
      await this.$refs.roleForm.validate()
      // 有id就是编辑
      if (this.roleForm.id) {
        await add(this.roleForm)
      } else {
        // 没有则是新增
        // 发请求
        await addRoleList(this.roleForm)
      }

      // 提示用户操作成功
      this.$message.success('操作成功')
      // 更新页面
      this.getRoleList()
      // 关闭弹窗
      this.showDialog = false
    }
  }
}
</script>

<style>

</style>
